----------------------------------------------------------------------------------------------------------------------------------------------------
--[[

【乾坤泰卦帽】自带一个特殊的【霉运】值，初始为0点，上限为777点。达到上限时，转变为【坤乾否卦帽】，内部所有配件立刻掉落。
当胡桃装备【乾坤泰卦帽】时，若雨露值高于21，则【霉运】每秒提升（X-21）*(1-Y)/7点，X为当前雨露值，Y为当前防水系数。
【乾坤泰卦帽】在晴天时且未被装备时，每秒减少【霉运】0.49点。在夏天或是附近7距离内有热源（篝火，营火，龙蝇火炉）时，每秒额外减少【霉运】0.49点。

【坤乾否卦帽】的【霉运】升降和【乾坤泰卦帽】相同逻辑。

当【乾坤泰卦帽】内部消耗速度。佩戴的时候才切换。
【霉运】在0-77的时候，戴着帽子的时候腐烂速度会变为7倍
【霉运】在77-343范围内时，内部食物腐败效率提升至10.5倍，并使佩戴者损失21san/min。
【霉运】大于343时，内部食物腐败效率提升至14倍，梅花饰品的耐久度消耗提升至2.1倍，并使佩戴者损失49san/min。

]]--
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 露水值刷新器
    local function bad_luck_update(inst,is_equipped,owner)
        if is_equipped and owner and owner.components.moisture.moisture > 21 and inst:HasTag("sky") then
            local X = owner.components.moisture.moisture
            local Y = inst.components.waterproofer and inst.components.waterproofer:GetEffectiveness() or 0
            if Y >= 1 then
                return
            end
            local delta = (X-21)*(1-Y)/7
            if TUNING.HUTAO_DEBUGGING_MODE then
                delta = 77
            end
            inst.components.hutao_com_cosmic_hat_sys_for_item:DoDelta(delta)
        elseif not is_equipped and not TheWorld.state.israining then
            local delta = -0.49
            if TheWorld.state.issummer or inst.components.hutao_com_flower_switcher:IsNearHotSource(7) then
                delta = -0.98
            end
            if TUNING.HUTAO_DEBUGGING_MODE then
                delta = -77
            end
            inst.components.hutao_com_cosmic_hat_sys_for_item:DoDelta(delta)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 内部使用倍率刷新器
    local function inner_use_mult_update(inst,is_equipped,owner)
        if not is_equipped or owner == nil then
            inst.components.hutao_com_cosmic_hat_sys_for_item:SetInnerUseMult(1)
            inst.components.preserver:SetPerishRateMultiplier(1)    -- 食物腐烂速度
            return
        end
        local value = inst.components.hutao_com_cosmic_hat_sys_for_item:GetCurrent()
        if value <= 77 then
            -- inst.components.hutao_com_cosmic_hat_sys_for_item:SetInnerUseMult(7)
            inst.components.preserver:SetPerishRateMultiplier(7)    -- 食物腐烂速度
        elseif value <= 343 then
            -- inst.components.hutao_com_cosmic_hat_sys_for_item:SetInnerUseMult(10.5)
            inst.components.preserver:SetPerishRateMultiplier(10.5)    -- 食物腐烂速度
            owner.components.sanity:DoDelta(-21/60,true)
        else
            inst.components.hutao_com_cosmic_hat_sys_for_item:SetInnerUseMult(2.1)
            inst.components.preserver:SetPerishRateMultiplier(14)    -- 食物腐烂速度
            owner.components.sanity:DoDelta(-49/60,true)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 乾坤泰卦帽 转换逻辑
    local sky_fn = function(inst)
        TheWorld.components.hutao_com_special_timer_for_theworld:AddTimer(inst,function(inst)  -- 使用绝对时间。因为要和天气同步
            local ower = inst.components.inventoryitem.owner
            local is_equipped = inst.components.equippable:IsEquipped()
            bad_luck_update(inst,is_equipped,ower)
            inner_use_mult_update(inst,is_equipped,ower)
        end,1)
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 坤乾否卦帽 转换逻辑
    local function ground_update_display_name(inst,_table)
        local current = _table and _table.new or inst.components.hutao_com_cosmic_hat_sys_for_item:GetCurrent()
        local owner = inst.components.hutao_com_cosmic_hat_sys_for_item:GetOwner()
        if owner == nil then return end
        --- 使用format 强制保留一位小数
        local display_num = string.format("%.1f",current)
        local hat_display_name = tostring(STRINGS.NAMES[string.upper(inst.prefab)])
        local player_name = owner:GetDisplayName()
        inst.components.named:SetName(hat_display_name .. " ( " .. player_name .. " )( "..display_num.." )")
    end
    local ground_fn = function(inst)
        TheWorld.components.hutao_com_special_timer_for_theworld:AddTimer(inst,function(inst)  -- 使用绝对时间。因为要和天气同步
            local ower = inst.components.inventoryitem.owner
            local is_equipped = inst.components.equippable:IsEquipped()
            bad_luck_update(inst,is_equipped,ower)
        end)
        inst:ListenForEvent("onbadluckchange",ground_update_display_name)
        inst:DoTaskInTime(0,ground_update_display_name)
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
return {
    sky = sky_fn,
    ground = ground_fn,
}

